
package flc.ast.adapter;


import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyFileBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class FileAdapter extends StkProviderMultiAdapter<MyFileBean> {

    public FileAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new FileProvider());
    }

    private boolean isEdit;

    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    private class FileProvider extends BaseItemProvider<MyFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyFileBean bean) {
            if (FileUtils.isDir(bean.getPath())) {
                baseViewHolder.getView(R.id.llFileItemFolder).setVisibility(View.VISIBLE);
                baseViewHolder.getView(R.id.llFileItemVideo).setVisibility(View.GONE);
                baseViewHolder.setText(R.id.tvFileItemFolderName, FileUtils.getFileName(bean.getPath()));
                List<File> files = FileUtils.listFilesInDir(bean.getPath());
                baseViewHolder.setText(R.id.tvFileItemFolderSize, getContext().getString(R.string.video_size_num, files.size()));
            } else {
                baseViewHolder.getView(R.id.llFileItemFolder).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.llFileItemVideo).setVisibility(View.VISIBLE);
                Glide.with(getContext()).load(bean.getPath())
                        .into((ImageView) baseViewHolder.getView(R.id.ivFileItemImg));
                baseViewHolder.setText(R.id.tvFileItemVideoName, FileUtils.getFileName(bean.getPath()));
                baseViewHolder.setText(R.id.tvFileItemVideoDate, TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()), new SimpleDateFormat("MM/dd HH:mm")));
                baseViewHolder.setText(R.id.tvFileItemVideoSize, ConvertUtils.byte2FitMemorySize(FileUtils.getFileLength(bean.getPath()), 0));
            }
            if (isEdit) {
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivFileItemFolderSel, R.drawable.xz);
                    baseViewHolder.setImageResource(R.id.ivFileItemVideoSel, R.drawable.xz);
                } else {
                    baseViewHolder.setImageResource(R.id.ivFileItemFolderSel, R.drawable.not_sel);
                    baseViewHolder.setImageResource(R.id.ivFileItemVideoSel, R.drawable.not_sel);
                }
            } else {
                baseViewHolder.setImageResource(R.id.ivFileItemFolderSel, R.drawable.jin);
                baseViewHolder.setImageResource(R.id.ivFileItemVideoSel, R.drawable.bf);
            }
        }
    }

}
